//
//  MKJAnimator.m
//  RedBookTransitionDemo
//
//  Created by MKJING on 16/11/17.
//  Copyright © 2016年 MKJING. All rights reserved.
//

#import "MKJAnimator.h"
//#import "MKJExplosionView.h"

#define PLAN 0

@implementation MKJAnimator

- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext
{
    return 0.25;
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
#if  PLAN
    if(self.isPush) {
        UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
        UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
        UIImage *image = [fromVC.view screenShot];
        UIImageView *snapView = [[UIImageView alloc] initWithImage:image];
        snapView.alpha = 1.0f;
        snapView.frame = fromVC.view.frame;
        //因为对截图做动画，vc1就可以隐藏了
        toVC.view.alpha = 0;
        fromVC.view.hidden = true;
        //这里有个重要的概念containerView，如果要对视图做转场动画，视图就必须要加入containerView中才能进行，可以理解containerView管理着所有做转场动画的视图
        UIView *containerView = [transitionContext containerView];
        //将截图视图和vc2的view都加入ContainerView中
        [containerView addSubview:snapView];
        [containerView addSubview:toVC.view];
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.originalRec];
        imageView.image = self.imageView.image;
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        
        //    UIImage *blur = [self.imageView.image applyExtraLightEffect];//[_resources blurredImageWithRadius:100 iterations:2.0f tintColor:[UIColor clearColor]];
        
        //    VideoDetailsView *detailsView = [[VideoDetailsView alloc] initWithFrame:self.isPush ? self.originalRec : self.destinationRec];
        //    detailsView.imageView.image = blur;
        //    UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:self.isPush ? self.originalRec : self.destinationRec];
        //    imageView2.image = blur;
        //    imageView2.contentMode = UIViewContentModeScaleAspectFill;
        //    [snapView addSubview:detailsView];
        //    [snapView addSubview:imageView2];
        [snapView addSubview:imageView];
        
        [UIView animateWithDuration:0.3 delay:0.0 options:0 animations:^{
            imageView.frame   = CGRectMake(0, 0, toVC.view.width, (toVC.view.height/2)+40);
            //        imageView2.frame = CGRectMake(0, CGRectGetMaxY(imageView.frame), toVC.view.width, toVC.view.height - imageView.height);
        } completion:nil];
        
        [UIView animateWithDuration:0.2 delay:0.3 options:0 animations:^{
            toVC.view.alpha = 1;
        } completion:^(BOOL finished) {
            [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
            [snapView  removeFromSuperview];
        }];
    } else {
//        return;
        UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
        UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
        UIView *containerView = [transitionContext containerView];
        toVC.view.hidden = false;
        fromVC.view.hidden = true;
        //参照present动画的逻辑，present成功后，containerView的最后一个子视图就是截图视图，我们将其取出准备动画
        UIImage *image = [toVC.view screenShot];
        UIImageView *snapView = [[UIImageView alloc] initWithImage:image];
        snapView.frame = fromVC.view.frame;
//        [containerView addSubview:fromVC.view];
        [containerView addSubview:snapView];
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, toVC.view.width, (toVC.view.height/2)+40)];
        imageView.image = self.imageView.image;
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        
        
        [snapView addSubview:imageView];
        
        //动画吧
        [UIView animateWithDuration:0.3f animations:^{
            
            imageView.frame   = self.destinationRec;
            //blurImage.frame   = _startFrame;
            //        detailsView.frame = _startFrame;
            
        }];
        [UIView animateWithDuration:0.2 delay:0.3 options:0 animations:^{
            snapView.alpha = 0;
        } completion:^(BOOL finished) {
            if ([transitionContext transitionWasCancelled]) {
                //失败了标记失败
                [transitionContext completeTransition:NO];
            }else{
                //如果成功了，我们需要标记成功，同时让vc1显示出来，然后移除截图视图，
                [transitionContext completeTransition:YES];
                fromVC.view.hidden = false;
                toVC.view.hidden = NO;
                [snapView removeFromSuperview];
            }
        }];
    }
    // 这是种最普通的动画，就是缩小而已
    //    UIViewController* toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    //    UIViewController* fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    //    [[transitionContext containerView] addSubview:toViewController.view];
    //    toViewController.view.alpha = 0;
    //
    //    [UIView animateWithDuration:1 animations:^{
    //        fromViewController.view.transform = CGAffineTransformMakeScale(0.1, 0.1);
    //        toViewController.view.alpha = 1;
    //    } completion:^(BOOL finished) {
    //        fromViewController.view.transform = CGAffineTransformIdentity;
    //        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
    //        if (self.delegate && [self.delegate respondsToSelector:@selector(animationFinish)]) {
    //            [self.delegate animationFinish];
    //        }
    //    }];
    
#else
    // 这个是小红书的动画  通过imageView实现rec的变化，然后消失，代理调用，再把image补上，动画就是衔接得这么不自然......
    UIView* contentView = [transitionContext containerView];
    contentView.backgroundColor = [UIColor whiteColor];
    UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    [contentView addSubview:toViewController.view];
//    toViewController.view.hidden = YES;
    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    fromVC.view.alpha = 1;
    
    CGFloat scale = self.destinationRec.size.width / self.originalRec.size.width;
    scale = self.isPush ? 1 / scale : scale;
    
    CGAffineTransform transform_to = toViewController.view.transform;
    transform_to = CGAffineTransformScale(transform_to, scale, scale);
    toViewController.view.transform = transform_to;
    toViewController.view.alpha = 0.5;
    
    __block UIImageView *imageView = [[UIImageView alloc] initWithImage:[_nodeView.view screenShot]];
    imageView.frame = self.isPush ? self.originalRec : self.destinationRec;
    [contentView addSubview:imageView];
    
    if (!self.isPush) {
        self.nodeView.alpha = 0;
    }
    
    [UIView animateWithDuration:1 delay:0 usingSpringWithDamping:3 initialSpringVelocity:3 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        imageView.frame = self.isPush ? self.destinationRec : self.originalRec;
        toViewController.view.transform = CGAffineTransformScale(transform_to, 1/scale, 1/scale);
        toViewController.view.alpha = 1;
        fromVC.view.alpha = 0.3;

//        fromVC.view.transform = CGAffineTransformScale(transform_to, 1/scale, 1/scale);

    } completion:^(BOOL finished) {
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
        [imageView removeFromSuperview];
        fromVC.view.alpha = 1;
        
        if (!self.isPush) {
            self.nodeView.alpha = 1;
        }
        else
        {
            if (self.delegate && [self.delegate respondsToSelector:@selector(animationFinish)]) {
                [self.delegate animationFinish];
            }
        }
        imageView = nil;
        
    }];
    
    
    //    [UIView animateWithDuration:0.5 animations:^{
    //
    //        imageView.frame = self.isPush ? self.destinationRec : self.originalRec;
    //        toViewController.view.alpha = 1.0f;
    //
    //    } completion:^(BOOL finished) {
    //        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
    //        [imageView removeFromSuperview];
    //        if (!self.isPush) {
    //            self.imageView.alpha = 1;
    //        }
    //        else
    //        {
    //            if (self.delegate && [self.delegate respondsToSelector:@selector(animationFinish)]) {
    //                [self.delegate animationFinish];
    //            }
    //        }
    //        imageView = nil;
    //    }];
#endif
}

@end
